home *** CD-ROM | disk | FTP | other *** search
- Path: vela.acs.oakland.edu!jggoslin
- From: jggoslin@vela.acs.oakland.edu (Monument)
- Newsgroups: comp.lang.c
- Subject: passing arrays and returning structs
- Date: 11 Mar 1996 20:05:28 GMT
- Organization: Oakland University, Rochester, Michigan, U.S.A.
- Message-ID: <4i2128$69d@news2.acs.oakland.edu>
- NNTP-Posting-Host: vela.acs.oakland.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- I have been wracking my brains over this one. I needed to write a
- program to do the transitive closure of a binary relation matrix, no
- problems encountered during that. However, I tried to streamline the
- code, by putting the transitive closure algorithm in it's own
- function. When I did this, I tried to pass the initially read in
- matrix into the function as an argument, and return a message buffer
- to the calling function.
-
- I got a mess of errors, included below for your perusal. If anyone
- has any suggestions on how I could get the pointers and returns fixed,
- I would sincerely appreciate it. Post or email, since I read both
- regularly.
-
- ===client.h===
- #include <stdio.h>
- #include <sys/errno.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/types.h>
-
- #define PATH "./server"
- #define PROJ 'B'
- #define PERMS 0666
- #define MAXSZ 200
-
- key_t key;
- int msgqid;
-
- typedef struct my_msgbuf {
- long mtype;
- int pid;
- int size;
- int data[2*MAXSZ];
- } Message;
- ===client.h===
-
- ===client.c===
- // header files
- #include "client.h"
-
- // function prototypes
- struct Message transitive_closure(int matrix[]);
-
- // MAINLINE
- int main(int argc, char *argv[])
- {
- [I am going to delete a bunch of code here that is irrelevant to the
- problem, since I know it works. All the code I delete does is reads
- in the matrix]
-
- [here is the relevant call]
- // get the transitive closure
- buffer = transitive_closure(matrix);
-
- // print out results
- puts("Transitive Closure:");
- for (i=0; i<buffer.size; i++)
- {
- for (j=0; j<buffer.size; j++)
- printf(" %d", buffer.data[i*buffer.size+j]);
- printf("\n");
- }
-
- // successful finish
- return 0;
- }
-
- struct Message* transitive_closure(int matrix[])
- {
- int i, j, size;
- Message buffer;
- pid_t pid;
-
- [here do I refer to the subtypes in the correct manner? "." vs. "->"
- is what I'm talking about]
-
- buffer.mtype=1L;
- buffer.pid=pid;
- buffer.size=size;
- for (i=0; i<size; i++)
- for (j=0; j<size; j++)
- buffer.data[i*size+j]=matrix[i*size+j];
-
- [is the return type correct?]
- return *buffer;
- }
- ===client.c===
-
- ===ERRORS===
- cc -g -c client.c
- /usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Functions cannot return a non-object type
- buffer = transitive_closure(matrix);
- ---------------------------^
- /usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Reference an expression of void type or an incomplete type.
- buffer = transitive_closure(matrix);
- ---------------------------^
- /usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: redeclaration of 'transitive_closure'; previous declaration at line 11 in file 'client.c'
- struct Message* transitive_closure(int matrix[])
- ----------------^
- /usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: Incompatible function return type for this function
- struct Message* transitive_closure(int matrix[])
- ----------------------------------^
- /usr/lib/cmplrs/cc/cfe: Error: client.c, line 109: Dereference a non-pointer
- return *buffer;
- --------^
- *** Exit 1
- Stop.
- ===ERRORS===
-
- ----------------------------------------------------------------------------
- | Jeff Goslin - Monument | "Oh Bentson, you are so |
- | jggoslin@vela.acs.oakland.edu | mercifully free from the |
- | | ravages of intellect." |
- | http://www.acs.oakland.edu/links/jggoslin | --Evil, The Time Bandits |
- ----------------------------------------------------------------------------
- | how come everyone elses religion is a cult but your cult is a religion |
- ----------------------------------------------------------------------------
-